package com.itboye.sunsun.person.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.itboye.homesensen.alert.ByAlert;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.support.BaseDialogFragment;
import com.itboye.sunsunhome.R;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.io.IOException;

import static com.itboye.sunsun.utils.SaveImageUtils.saveBitmap;

/**
 * @author 废弃
 */
@SuppressLint("NewApi")
public class NewsDetailShareFragment extends BaseDialogFragment implements OnClickListener {

    //	private static final String APP_ID = "wxa30658c4f4f6bbf4";
    private IWXAPI api;
    private String id;
    private String type;
    private String uid;
    private String password;
    private String content;
    private String url;
    private Bundle bundle;
    private final String APPID = SPContants.TencentAppId;
    Tencent mTencent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 取消标题栏

        // setStyle(DialogFragment.STYLE_NO_FRAME,R.style.shareDialog);

        url = getTag();
        // 设置底部显示
//		uid = "uid" + "=" + (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, "");
//		password = "psw" + "=" + (String) SPUtils.get(App.ctx, null, SPContants.PASSWORD, "");
        //设置监听事件
        View view = inflater.inflate(R.layout.share_fragment, container);
        RelativeLayout sinaRela = (RelativeLayout) view
                .findViewById(R.id.sinarela);
        sinaRela.setOnClickListener(this);
        RelativeLayout wxRela = (RelativeLayout) view.findViewById(R.id.werela);
        mTencent = Tencent.createInstance(APPID, App.ctx);
//		mTencent.startAppbar(getActivity(), AppbarAgent.TO_APPBAR_DETAIL);
        wxRela.setOnClickListener(this);
        RelativeLayout wecircleRela = (RelativeLayout) view
                .findViewById(R.id.wecirclerela);
        wecircleRela.setOnClickListener(this);
        api = WXAPIFactory.createWXAPI(App.ctx, App.WX_APP_ID);


        return view;
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        UMImage image = new UMImage(App.ctx,
                "http://www.umeng.com/images/pic/social/integrated_3.png");
        switch (id) {
            case R.id.sinarela:
                shareToQQ();
                break;
            case R.id.werela:
                shareToFriendOrFriends(SendMessageToWX.Req.WXSceneSession);
                break;
            case R.id.wecirclerela:
                shareToFriendOrFriends(SendMessageToWX.Req.WXSceneTimeline);
                break;

            default:
                break;
        }
//		getActivity().finish();
    }

    private void shareToQQ() {
        String fileName="";
        Bitmap bitmap=BitmapFactory.decodeResource(App.ctx.getResources(),R.drawable.sunsun_img);
        try {
            fileName=saveBitmap(bitmap,"logo.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        bundle = getArguments();
        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, bundle.getString("title"));
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, bundle.getString("content"));
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, fileName);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, getString(R.string.app_name));
//		    params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,  "其他附加功能");		
        mTencent.shareToQQ(getActivity(), params, new IUiListener() {

            @Override
            public void onError(UiError arg0) {
                // TODO Auto-generated method stub
//					Toast.makeText(getActivity(), ">>>12"+arg0, 0).show();
            }

            @Override
            public void onComplete(Object arg0) {
                // TODO Auto-generated method stub
//					Toast.makeText(getActivity(), ">>>"+arg0, 0).show();
            }

            @Override
            public void onCancel() {
                // TODO Auto-generated method stub
//					Toast.makeText(getActivity(), "已取消", 0).show();
            }
        });

    }


    //分享
    public void shareToFriendOrFriends(int type) {
        bundle = getArguments();
        WXWebpageObject webpage0 = new WXWebpageObject();
        webpage0.webpageUrl = url;
        //标题
        WXMediaMessage msg0 = new WXMediaMessage(webpage0);
        msg0.title = bundle.getString("title");
        msg0.description = bundle.getString("content");
        //商品图片
        Bitmap thumb0 = BitmapFactory.decodeResource(getResources(), R.drawable.sunsun_img);
        msg0.setThumbImage(thumb0);

        //构建一个请求
        SendMessageToWX.Req req0 = new SendMessageToWX.Req();
        req0.message = msg0;
        req0.scene = type;//WXSceneTimeline分享至朋友圈
        req0.transaction = "sessionwebpage";
        api.sendReq(req0);
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            // Toast.makeText(ShareActivity.this, platform + " 分享成功啦",
            // Toast.LENGTH_SHORT).show();
            ByAlert.alert("分享成功！");
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            // Toast.makeText(ShareActivity.this,platform + " 分享失败啦",
            // Toast.LENGTH_SHORT).show();
            ByAlert.alert("分享失败！");
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            // Toast.makeText(ShareActivity.this,platform + " 分享取消了",
            // Toast.LENGTH_SHORT).show();
            ByAlert.alert("已取消分享！");
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(App.ctx).onActivityResult(requestCode, resultCode, data);
    }

}
